package com.xhwl.logistics.enums;

import com.xhwl.common.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

@Getter
@AllArgsConstructor
public enum VehicleCarTypeEnum {

    CAR(1, "汽车"),
    MEDIUM_AND_LARGE_CAR(2, "中大型汽车"),
    POLICE_CAR(3, "警车"),
    NEW_ENERGY_CAR(4, "新能源车"),
    MEDIUM_AND_LARGE_BUS(5, "中大型客车"),
    TRUCK(6, "货车"),
    MOTORCYCLE(7, "摩托车");

    private static final Map<Integer, VehicleCarTypeEnum> mapForId;
    private static final Map<String, VehicleCarTypeEnum> mapForDescription;
    static {
        mapForId = new HashMap<>();
        for (VehicleCarTypeEnum vehicleCarTypeEnum : VehicleCarTypeEnum.values()) {
            mapForId.put(vehicleCarTypeEnum.getId(), vehicleCarTypeEnum);
        }

        mapForDescription = new HashMap<>();
        for (VehicleCarTypeEnum vehicleCarTypeEnum : VehicleCarTypeEnum.values()) {
            mapForDescription.put(vehicleCarTypeEnum.getDescription(), vehicleCarTypeEnum);
        }
    }

    private final Integer id;

    private final String description;

    public static VehicleCarTypeEnum toVehicleCarTypeEnum(Integer id) {
        return Optional.ofNullable(mapForId.get(id)).orElseThrow(() -> new BusinessException("非法的车辆类型id"));
    }

    public static VehicleCarTypeEnum toVehicleCarTypeEnum(String description) {
        return Optional.ofNullable(mapForDescription.get(description)).orElseThrow(() -> new BusinessException("非法的车辆类型名称"));
    }

}
